package com.chandra.pos.customer.dao;

import com.chandra.pos.customer.dao.entity.Customer;
import com.chandra.pos.customer.exception.CustomerNotFoundException;
import com.chandra.pos.customer.model.CustomerSearchCriteria;
import com.chandra.pos.customer.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import javax.inject.Named;
import java.util.List;

/**
 * 
 * Customer DAO
 * 
 * @author onlinetechvision.com
 * @since 25 Mar 2012
 * @version 1.0.0
 *
 */

@Named
public class CustomerDAO implements ICustomerDAO {



    @Autowired
    private CustomerRepository customerRepository;
	/**
	 * Add Customer
	 * 
	 * @param  User customer
	 */
	@Override
	public void addUser(Customer customer) {
        customerRepository.save(customer);
	}

	/**
	 * Delete Customer
	 * 
	 * @param  User customer
	 */
	@Override
	public void deleteUser(Customer customer) {
        customerRepository.delete(customer);
	}

	/**
	 * Update Customer
	 * 
	 * @param  User customer
	 */
	@Override
	public void updateUser(Customer customer) {
        customerRepository.save(customer);
	}

	/**
	 * Get Customer
	 * 
	 * @param  int Customer Id
	 * @return Customer
	 */
	@Override
	public Customer getUserById(Long id) {

       return customerRepository.findOne(id);


	}

	/**
	 * Get Customer List
	 * 
	 * @return List - Customer list
	 */
	@Override
	public Page getUsers(PageRequest posPagination) {


       Page list = customerRepository.findAll(posPagination);
		return list;
	}

    @Override
    public List<Customer> searchUsers(CustomerSearchCriteria searchCriteria) {

        if(searchCriteria.getQuery() !=null)
          return   customerRepository.search(searchCriteria.getQuery().toLowerCase()+"%");

        if(searchCriteria.getFilters() !=null)
            return     customerRepository.search(searchCriteria);

        return customerRepository.search(searchCriteria);  //To change body of implemented methods use File | Settings | File Templates.
    }


    public Customer getCustomerDetail(String firstName, String lastName, String city) throws CustomerNotFoundException {
        Customer customer=    customerRepository.findByFirstNameAndLastNameAndCity(firstName, lastName, city);

        if(customer ==null )
            throw new  CustomerNotFoundException();

        return customer;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Customer getCustomerDetail(Long id) {
        return customerRepository.findOne(id);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Customer markAsCustomer(Customer customer) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Customer> searchUsers(String s) {
        return customerRepository.search(s);  //To change body of implemented methods use File | Settings | File Templates.
    }

}
